﻿<cfsilent>
	<cfscript>
		
		event.setArg("pageTitle", "行政班课表");
		
		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		
		/* 设置为横向打印 */
		event.setArg("portrait", false);
		
		termId = event.getArg("TID");
		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");
		
		subjectId = event.getArg("SBJ");
		grade = event.getArg("GRD");

		sql = "	SELECT
					clz.cls_id,clz.cls_name,i.institute_name,sbj.sbj_name,clz.grade
				FROM
					t_class clz
					INNER JOIN t_subject sbj ON sbj.sbj_id = clz.sbj_id
					INNER JOIN t_institute i ON i.institute_id = sbj.institute_id
				WHERE
					sbj.sbj_id = :subjectId 
					AND
					clz.grade = :grade 
				ORDER BY
					clz.cls_name";
			
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "subjectId", value = subjectId, cfsqltype = "cf_sql_varchar");
		queryObj.addParam(name = "grade", value = grade, cfsqltype = "cf_sql_char");
		rs_clazz = queryObj.execute(sql = sql).getResult();
		
		
	</cfscript>
</cfsilent>

<cfset currentPage = 1 />

<cfloop query="rs_clazz">
	
	<cfif currentPage GT 1><div class="page_break"></div></cfif>
	
	<!--- 单个班级课表 --->
	<cfset tagCode = "XZBKB-" & termId & "," & rs_clazz.cls_id />	
	
	<div class="UIprintableTitle"><cfoutput>#academicYear#</cfoutput>-<cfoutput>#academicTerm#</cfoutput>学期 <cfoutput>#rs_clazz.cls_name#</cfoutput> 班级课表</div>
	<table width="98%" border="0" cellpadding="1" cellspacing="0">
		<tr>
			<td>学院: <cfoutput>#rs_clazz.institute_name#</cfoutput></td>
			<td>专业: <cfoutput>#rs_clazz.sbj_name#</cfoutput></td>
			<td align="right">年级: <cfoutput>#rs_clazz.grade#</cfoutput></td>
		</tr>
	</table>
	
	<cfscript>
		
		/* 读取班级学生课表 */
		sql = "	SELECT
					DISTINCT 
					c.course_name,
					a.tasktime,
					CASE WHEN a.taskclsroom IS NULL THEN '-' ELSE a.taskclsroom END taskclsroom,
					a.task_week,
					ta.task_type,ta.task_id
				FROM
					t_task_timetable a INNER JOIN t_task_arrange ta ON ta.task_id = a.task_id INNER JOIN t_course c ON c.cid = ta.cid
				WHERE
					EXISTS(
					   SELECT 1 FROM t_student_course b INNER JOIN t_student stu ON stu.stu_id = b.stu_id
					   WHERE
						   ta.task_id = b.task_id
						   AND
						   ta.term_id = :termId 
						   AND
						   stu.cls_id = :classId 
						   AND
						   b.st = 'N'
						   AND
						   b.task_type = 'P'
					)";
			
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
		queryObj.addParam(name = "classId", value = rs_clazz.cls_id, cfsqltype = "cf_sql_char");
		rs_tableData = queryObj.execute(sql = sql).getResult();
		
		timeTable = arrayNew(2);
		
		/* 课表冲突标记 */
		for ( lec=1; lec lte 6; lec++ ) {
			for ( wkday=1; wkday lte 7; wkday++ ) {
				timeTable[wkday][lec] = 0;
			}
		}
		
		timeTableSet = structNew();
		
		/* 生成课表显示数据 */
		//for ( rs in tableData ) {
		for(a = 1; a lte rs_tableData.recordCount; a++){
			
			currentTaskTime = rs_tableData['tasktime'][a];
			
			if ( rs_tableData['taskclsroom'][a] eq "-" ) { 
				currentTaskPlace = "";
			}
			else {
				currentTaskPlace = rs_tableData['taskclsroom'][a];
			}
			
			wkday = listGetAt(currentTaskTime, 1, "-@");
			lec = listGetAt(currentTaskTime, 2, "-@");
			cell = listGetAt(currentTaskTime, 3, "-@");
			
			/* 载入教学任务 */
			switch(rs_tableData['task_type'][a]){
				case 'P':
					sql = "	SELECT
								f.tch_name f_tch_name,
								s.tch_name s_tch_name,
								t.tch_name t_tch_name
							FROM
								t_task tsk 
								INNER JOIN t_teacher f ON f.tch_id = tsk.tch_id
								INNER JOIN t_teacher s ON s.tch_id = tsk.tch_id_sec
								INNER JOIN t_teacher t ON t.tch_id = tsk.tch_id_ace
							WHERE
								tsk.tsk_id = :taskId ";
					break;
				case 'R':
					sql = "	SELECT
								f.tch_name f_tch_name,
								s.tch_name s_tch_name,
								t.tch_name t_tch_name
							FROM
								t_reopen_task tsk 
								INNER JOIN t_teacher f ON f.tch_id = tsk.tch_id
								INNER JOIN t_teacher s ON s.tch_id = tsk.tch_id_sec
								INNER JOIN t_teacher t ON t.tch_id = tsk.tch_id_ace
							WHERE
								tsk.tsk_id = :taskId ";
					break;
				case 'C':
					sql = "	SELECT
								f.tch_name f_tch_name,
								s.tch_name s_tch_name,
								t.tch_name t_tch_name
							FROM
								t_common_task tsk 
								INNER JOIN t_teacher f ON f.tch_id = tsk.tch_id
								INNER JOIN t_teacher s ON s.tch_id = tsk.tch_id_sec
								INNER JOIN t_teacher t ON t.tch_id = tsk.tch_id_ace
							WHERE
								tsk.tsk_id = :taskId ";
					break;
				case 'T':
					sql = "	SELECT
								f.tch_name f_tch_name,
								s.tch_name s_tch_name,
								t.tch_name t_tch_name
							FROM
								t_sport_task tsk 
								INNER JOIN t_teacher f ON f.tch_id = tsk.tch_id
								INNER JOIN t_teacher s ON s.tch_id = tsk.tch_id_sec
								INNER JOIN t_teacher t ON t.tch_id = tsk.tch_id_ace
							WHERE
								tsk.tsk_id = :taskId ";
					break;
			}
				
			queryObj = new Query(datasource = application.dnsSlave);
			queryObj.addParam(name = "taskId", value = rs_tableData['task_id'][a], cfsqltype = "cf_sql_varchar");
			rs_taskEntity = queryObj.execute(sql = sql).getResult();
				
			/* 生成教师 */
			currentTeacher = rs_taskEntity.f_tch_name;
			
			if ( not listFind( currentTeacher, rs_taskEntity.s_tch_name, "/ " ) ) {
				currentTeacher = currentTeacher & " / " & rs_taskEntity.s_tch_name;
			}
			
			if ( not listFind( currentTeacher, rs_taskEntity.t_tch_name, "/ " ) ) {
				currentTeacher = currentTeacher & " / " & rs_taskEntity.t_tch_name;
			}
			
			/* 
				生成课表内容 
				cellCourse = [ courseName, teacherName, place, cellLength, taskWeek]
			*/
			if ( cell eq 2 ) {

				cellCourse = [ rs_tableData['course_name'][a], currentTeacher, currentTaskPlace, cell, rs_tableData['task_week'][a] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
			
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
			
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
		
			}
			
			if ( cell eq 3 and lec eq 2 ) {

				cellCourse = [ rs_tableData['course_name'][a], currentTeacher, currentTaskPlace, cell, rs_tableData['task_week'][a] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
			
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
					
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
			}
			
			if ( cell eq 3 and lec eq 5 ) {

				cellCourse = [ rs_tableData['course_name'][a], currentTeacher, currentTaskPlace, 2, rs_tableData['task_week'][a] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
			
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
		
				/* 晚上3节延续1学时 */
				cellCourse = [ rs_tableData['course_name'][a], currentTeacher, currentTaskPlace, 1, rs_tableData['task_week'][a] ];
			
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
			
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
			
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
			}
			
			/* 4学时延续 */
			if ( cell eq 4 ) {
				
				cellCourse = [ rs_tableData['course_name'][a], currentTeacher, currentTaskPlace, 2, rs_tableData['task_week'][a] ];
		
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
			
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
			
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
					}
			
			/* 5学时延续 */
			if ( cell eq 5 ) {
				
				cellCourse = [ rs_tableData['course_name'][a], currentTeacher, currentTaskPlace, 2, rs_tableData['task_week'][a] ];
				
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				cellCourse = [ rs_tableData['course_name'][a], currentTeacher, currentTaskPlace, 3, rs_tableData['task_week'][a] ];
				
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
			}
			
			/* 8学时延续 */
			if ( cell eq 8 ) {
				
				cellCourse = [ rs_tableData['course_name'][a], currentTeacher, currentTaskPlace, 2, rs_tableData['task_week'][a] ];
				
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
			
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				lectureKey = "KB" & wkday & lec+2;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+2] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				lectureKey = "KB" & wkday & lec+3;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+3] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}


			}

		}
		
	</cfscript>

	<!-- courseTable -->
	<table class="UIprintable noborder" width="100%" border="1" cellpadding="3" cellspacing="1">
		<thead>
			<tr class="noborder">
				<td colspan="2" width="2%"></td>
				<td width="14%">星期一</td>
				<td width="14%">星期二</td>
				<td width="14%">星期三</td>
				<td width="14%">星期四</td>
				<td width="14%">星期五</td>
				<td width="14%">星期六</td>
				<td width="14%">星期天</td>
			</tr>
		</thead>
		<tbody>
			<cfloop from="1" to="6" index="lec">
				<tr>
					<cfswitch expression="#lec#">
						<cfcase value="1">
						<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
						</cfcase>
						<cfcase value="3">
						<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
						</cfcase>
						<cfcase value="5">
						<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
						</cfcase>
					</cfswitch>
					<td><cfoutput>#mathAdvice.convertLectureIdxToChinese(lec)#</cfoutput></td>
					<cfloop from="1" to="7" index="wkday">
						<cfset lectureKey = "KB" & wkday & lec />
						<td class="timeCell <cfif timeTable[wkday][lec]>attention</cfif>">
							<cfif structKeyExists( timeTableSet, lectureKey )>
								<cfset i = 0 />
								<cfloop array="#timeTableSet[lectureKey]#" index="cellCourse">
									<cfset i++ />
									<div class="lecture"> 
										<span class="course"><cfoutput>#cellCourse[1]#</cfoutput></span> - <span class="teacher"><cfoutput>#cellCourse[2]#</cfoutput></span> 
										<br/>
										<span class="week"><cfoutput>#cellCourse[5]#</cfoutput>(<cfoutput>#cellCourse[4]#</cfoutput>)</span>
										<cfif cellCourse[3] neq "-">
											<br/>
											<span class="place"><cfoutput>#cellCourse[3]#</cfoutput></span>
										</cfif>
									</div>
									<cfif i lt arrayLen(timeTableSet[lectureKey]) >
										<div class="cellBreaker"><!--.--></div>
									</cfif>
								</cfloop>
							</cfif>
						</td>
					</cfloop>
				</tr>
			</cfloop>
		</tbody>
	</table>
	<!-- /courseTable -->
	
	<div class="clear2px"></div>
	<table width="100%" cellpadding="0" cellspacing="0">
		<tr>
			<td>打印时间 <cfoutput>#dateFormat(now(), "YYYY年MM月DD日")#</cfoutput></td>
			<td align="right"><img src="<cfoutput>#getContextRoot()#</cfoutput>/barcode/Print/<cfoutput>#tagCode#</cfoutput>.png" height="43"/></td>
		</tr>
	</table>
	
	<cfset currentPage++ />
	
</cfloop>